package com.etime.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Month;
public class BaseServlet extends HttpServlet {
  @Override
  protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=utf-8");
    String flag = req.getParameter("flag");
    //获取当前字节码对象
    Class clazz = this.getClass();
      //flag其实就是要执行的方法的名字
      //所有需要执行的方法的参数都是request和response
      //根据方法名及其参数的数据类型获取要执行的方法
    try {
      Method method = clazz.getDeclaredMethod(flag, HttpServletRequest.class, HttpServletResponse.class);
      method.setAccessible(true);
      method.invoke(this,req , resp);
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
  }
}
